<template>
	<view class="start-page-con">
		<view class="app-title">
			欢迎使用医疗帮APP
		</view>
		<view class="agreement-info">
			<view class="service-title">
				隐私保护与服务协议
			</view>
			<view class="text-con">
				本应用尊重并保护所有用户的个人隐私权。为了给您提供更准确、更有个性化的服务，本应用会按隐私政策的规定使用和披露您的个人信息。
			</view>
			<view class="agree-title" @tap="toServiceAgreement()">
				阅读《服务协议》
			</view>
			<view class="agree-title" @tap="toServiceAgreement()">
				阅读《隐私政策》
			</view>
		</view>
		
		<view class="padding flex flex-direction btn-oper">
			<button class="cu-btn bg-blue lg" 
				style="background-color: #E96141; margin-bottom: 30upx;" 
				@tap="agreeAndAuth()">同意并授权</button>
			<button class="cu-btn line-orange lg"
				@tap="noAndQuit()">我不同意并退出应用</button>
				
			<!-- <view>
				per_read_phone_state: #{{per.per_read_phone_state}}#
			</view>
			<view>
				per_call_phone: #{{per.per_call_phone}}#
			</view>
			<view>
				per_write_external_storage: #{{per.per_write_external_storage}}#
			</view>
			<view>
				hasOpenMustPermission: #{{per.hasOpenMustPermission}}#
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		APP_MUST_PERMISSION_STATUS,
		USER_HAS_LOGIN,
	} from "@/common/util/constants"
	import mobilePermission from '@/common/util/wa-permission/permission.js'
	
	export default {
		name: 'startPage',
		data() {
			return {
				per: {
					per_read_phone_state: 0,
					//per_call_phone: 0,
					per_write_external_storage: 0,
					hasOpenMustPermission: false,
				},
			};
		},
		beforeCreate(){
		},
		created() {
			//console.log('SP created()')
			
			let app_must_permission_status = uni.getStorageSync(APP_MUST_PERMISSION_STATUS)
			//console.log('SP beforeCreate() app_must_permission_status = ', app_must_permission_status)
			if(app_must_permission_status && app_must_permission_status=='1'){
				this.agreeAndAuth()
				//this.toNextPage()
			}
		},
		methods:{
			//服务协议-查看
			toServiceAgreement(){
				uni.navigateTo({
					url: '/pages/singles/service-agreement'
				})
			},
			//同意协议-开始授权
			async agreeAndAuth(){
				let that = this
				// #ifdef H5
				uni.redirectTo({
					url: '/pages/index/index'
				})
				// #endif
				
				// #ifdef APP-PLUS
				/*
				开始验证手机是否开心必要权限
					requestAndroidPermission('')
						 1: 已获取授权
						 0: 未获取授权
						-1: 被永久拒绝授权
				*/
				let hasOpenMustPermission = true  //2022-3-1 10:05
				let permissionIdStr = ''
				
				//允许程序访问电话状态
				permissionIdStr = 'android.permission.READ_PHONE_STATE'
				let per_read_phone_state = await mobilePermission.requestAndroidPermission(permissionIdStr)
				/* that.per.per_read_phone_state = per_read_phone_state
				console.log('start-page per_read_phone_state = ', per_read_phone_state) */
				if(per_read_phone_state==1){
				}else{
					uni.showToast({
						title: '请手动授权[允许程序访问电话状态]',
						mask: true,
						icon: 'none',
						duration: 2000
					});
					//return true
				}
				
				//允许程序从非系统拨号器里拨打电话
				/* permissionIdStr = 'android.permission.CALL_PHONE'
				let per_call_phone = await mobilePermission.requestAndroidPermission(permissionIdStr)
				console.log('start-page per_call_phone = ', per_call_phone)
				that.per.per_call_phone = per_call_phone
				if(per_call_phone==1){
				}else{
					uni.showToast({
						title: '请手动授权[允许程序辅助电话拨号]',
						mask: true,
						icon: 'none',
						duration: 2000
					});
					return false
				} */
				
				//允许程序写入外部存储,如SD卡上写文件
				permissionIdStr = 'android.permission.WRITE_EXTERNAL_STORAGE'
				let per_write_external_storage = await mobilePermission.requestAndroidPermission(permissionIdStr)
				/* that.per.per_write_external_storage = per_write_external_storage
				console.log('start-page per_write_external_storage = ', per_write_external_storage) */
				if(per_write_external_storage==1){
				}else{
					uni.showToast({
						title: '请手动授权[允许程序读写外部存储]',
						mask: true,
						icon: 'none',
						duration: 2000
					});
					//return true
				}
				
				if(per_read_phone_state==1 && per_write_external_storage==1){
					//hasOpenMustPermission = true  2022-3-1 
				}
				//that.per.hasOpenMustPermission = hasOpenMustPermission
				
				/* let _title = '' + 
					'1:' + per_read_phone_state + 
					//',2:' + per_call_phone + 
					',3:' + per_write_external_storage + 
					',4:' + hasOpenMustPermission 
				uni.showToast({
					title: _title,
					mask: true,
					icon: 'none',
					duration: 3000
				}); */
				if(hasOpenMustPermission){
					uni.setStorageSync('APP_MUST_PERMISSION_STATUS', '1')
					/* console.log('SP 已授权')
					uni.showToast({
						title: 'SP已授权',
						mask: true,
						icon: 'none',
						duration: 1500
					}); */
					//return false
					that.toNextPage()
				}else{
					/* console.log('SP未授权')
					uni.showToast({
						title: 'SP未授权',
						mask: true,
						icon: 'none',
						duration: 1500
					}); */
				}
				// #endif
			},
			//去下个页面
			toNextPage(){
				let user_has_login = uni.getStorageSync(USER_HAS_LOGIN)
				if(user_has_login=='1'){
					/* uni.showToast({
						title: 'SP去首页',
						mask: true,
						icon: 'none',
						duration: 1500
					}) */
					uni.redirectTo({
						url: '/pages/index/index'
					})
				}else{
					/* uni.showToast({
						title: 'SP去登录页',
						mask: true,
						icon: 'none',
						duration: 1500
					}) */
					uni.redirectTo({
						url: '/pages/login/login'
					})
				}
			},
			//不同意-退出应用
			noAndQuit(){
				// #ifdef APP-PLUS
				plus.runtime.quit();
				// #endif
			},
			
		}
	}
</script>

<style lang="scss" scoped>
.start-page-con{
	padding: 150upx 20upx 20upx 20upx;
	.app-title{
		font-size: 48upx;
		text-align: center;
		margin: 50upx 0;
		color: #666666;
	}
	.agreement-info{
		font-size: 32upx;
		color: #666666;
		background-color: #ffffff;
		border-radius: 15upx;
		padding: 20upx;
		box-shadow: 0 2upx 10upx rgba(0,0,0,0.07);
		.service-title{
			font-size: 40upx;
			font-weight: bold;
			color: #E96141;
			text-align: center;
			margin: 30upx 0;
		}
		.text-con{
			text-indent: 30px;
			line-height: 25px;
		}
		.agree-title{
			text-align: center;
			margin: 20upx 0;
			color: #1CBBB4;
		}
	}
	
	.btn-oper{
		margin-top: 60upx;
	}
}
</style>
